home *** CD-ROM | disk | FTP | other *** search
/ Borland JBuilder 6 / jbuilder6.iso / Taiji Applet Pack v2.7 / RandomImage / RandomImage.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-11-06  |  8.8 KB  |  388 lines

  1. import java.applet.Applet;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Component;
  5. import java.awt.Cursor;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics;
  9. import java.awt.Image;
  10. import java.awt.MediaTracker;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13. import java.net.MalformedURLException;
  14. import java.net.URL;
  15. import java.net.URLEncoder;
  16.  
  17. public class RandomImage extends Applet implements Runnable, MouseListener {
  18.    private Image image;
  19.    private int n_image;
  20.    // $FF: renamed from: wi int
  21.    private int field_0;
  22.    // $FF: renamed from: he int
  23.    private int field_1;
  24.    private int decx;
  25.    private int decy;
  26.    private int wi_im;
  27.    private int he_im;
  28.    private Thread thread;
  29.    private boolean loaded = false;
  30.    private String image_name;
  31.    // $FF: renamed from: b java.awt.Graphics
  32.    private Graphics field_2;
  33.    private Image buffer;
  34.    private String statusBarText;
  35.    private AudioClip sound;
  36.    private AudioClip clicSound;
  37.    private AudioClip enterSound;
  38.    private boolean loopSound;
  39.    private String link;
  40.    // $FF: renamed from: fm java.awt.FontMetrics
  41.    private FontMetrics field_3;
  42.    private String enterText;
  43.    private boolean isEnterTextEnabled;
  44.    private int wiEnterText;
  45.    private int enterTextHeight;
  46.    private Color enterTextColor;
  47.  
  48.    public String getAppletInfo() {
  49.       return "Name: RandomImage\r\nAuthor: Taiji Software\r\n";
  50.    }
  51.  
  52.    public RandomImage() {
  53.       ((Component)this).addMouseListener(this);
  54.    }
  55.  
  56.    public void register() {
  57.       try {
  58.          URL u = new URL("http://www.taijisoftware.com");
  59.          ((Applet)this).getAppletContext().showDocument(u, "_blank");
  60.       } catch (Exception e) {
  61.          System.out.println(e);
  62.          this.stop();
  63.       }
  64.    }
  65.  
  66.    public void init() {
  67.       String codeBase = null;
  68.  
  69.       try {
  70.          codeBase = (new URL(((Applet)this).getCodeBase().toString())).getHost();
  71.          System.out.println("Copyright 1999, 2001 Taiji Software(tm)\nYour domain name is : " + codeBase);
  72.          codeBase = codeBase.toUpperCase();
  73.       } catch (Exception var9) {
  74.       }
  75.  
  76.       if (!((Applet)this).getCodeBase().toString().toUpperCase().startsWith("FILE") || ((Applet)this).getParameter("debug") != null) {
  77.          String regCode = ((Applet)this).getParameter("registration_code");
  78.          if (regCode == null) {
  79.             regCode = ((Applet)this).getParameter("reg_domain");
  80.             if (regCode == null) {
  81.                this.register();
  82.             } else {
  83.                if (regCode.length() == codeBase.length() + 4 && !codeBase.startsWith("WWW.")) {
  84.                   codeBase = "WWW." + codeBase;
  85.                } else if (regCode.length() == codeBase.length() - 4 && codeBase.startsWith("WWW.")) {
  86.                   codeBase = codeBase.substring(4);
  87.                }
  88.  
  89.                char[] chars = new char[codeBase.length()];
  90.                codeBase.getChars(0, codeBase.length(), chars, 0);
  91.                String key = new String("haricot");
  92.                char[] chars2 = new char[key.length()];
  93.                key.getChars(0, key.length(), chars2, 0);
  94.  
  95.                for(int i = 0; i < codeBase.length(); ++i) {
  96.                   int j;
  97.                   if (i >= key.length()) {
  98.                      j = i - key.length() * (i / key.length());
  99.                   } else {
  100.                      j = i;
  101.                   }
  102.  
  103.                   chars[i] += chars2[j];
  104.                   chars[i] = (char)(chars[i] - chars[i] / 26 * 26 + 97);
  105.                }
  106.  
  107.                String res = new String(chars);
  108.                if (!res.equalsIgnoreCase(regCode)) {
  109.                   this.register();
  110.                }
  111.             }
  112.          } else if (!regCode.equals("settevercsedegnamiaj") && !regCode.equals("8078")) {
  113.             this.register();
  114.          }
  115.       }
  116.  
  117.       this.getParameters();
  118.       if (this.sound != null) {
  119.          if (this.loopSound) {
  120.             this.sound.loop();
  121.             return;
  122.          }
  123.  
  124.          this.sound.play();
  125.       }
  126.  
  127.    }
  128.  
  129.    public void getParameters() {
  130.       int image_number = 0;
  131.       String s = ((Applet)this).getParameter("images_number");
  132.       if (s != null) {
  133.          image_number = Integer.parseInt(s);
  134.       }
  135.  
  136.       this.field_0 = ((Component)this).getSize().width;
  137.       this.field_1 = ((Component)this).getSize().height;
  138.       double[] pourcent = new double[image_number + 1];
  139.  
  140.       for(int i = 1; i <= image_number; ++i) {
  141.          String var10 = null;
  142.          String var11 = ((Applet)this).getParameter("pourcent" + Integer.toString(i));
  143.          if (var11 != null) {
  144.             pourcent[i] = (double)Integer.parseInt(var11);
  145.          } else {
  146.             pourcent[i] = (double)(100 / image_number);
  147.          }
  148.       }
  149.  
  150.       double r = (double)100.0F * Math.random();
  151.       double t = (double)0.0F;
  152.  
  153.       for(int i = 1; i <= image_number; ++i) {
  154.          t += pourcent[i];
  155.          if (r < t) {
  156.             this.n_image = i;
  157.             break;
  158.          }
  159.       }
  160.  
  161.       if (this.n_image < 1 || this.n_image > image_number) {
  162.          this.n_image = (int)((double)image_number * Math.random()) + 1;
  163.       }
  164.  
  165.       this.image_name = ((Applet)this).getParameter("image" + Integer.toString(this.n_image));
  166.       ((Component)this).setBackground(this.getColor("background_color"));
  167.       this.statusBarText = ((Applet)this).getParameter("status_bar_text");
  168.       s = ((Applet)this).getParameter("sound_name");
  169.       if (s != null) {
  170.          this.sound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  171.       }
  172.  
  173.       s = ((Applet)this).getParameter("loop_sound");
  174.       if (s != null) {
  175.          if (s.equals("yes")) {
  176.             this.loopSound = true;
  177.          } else {
  178.             this.loopSound = false;
  179.          }
  180.       }
  181.  
  182.       s = ((Applet)this).getParameter("clic_sound_name");
  183.       if (s != null) {
  184.          this.clicSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  185.       }
  186.  
  187.       s = ((Applet)this).getParameter("enter_sound_name");
  188.       if (s != null) {
  189.          this.enterSound = ((Applet)this).getAudioClip(((Applet)this).getCodeBase(), s);
  190.       }
  191.  
  192.       this.link = ((Applet)this).getParameter("link");
  193.       this.enterText = ((Applet)this).getParameter("enter_text");
  194.       s = ((Applet)this).getParameter("enter_text_height");
  195.       if (s != null) {
  196.          this.enterTextHeight = Integer.parseInt(s);
  197.       }
  198.  
  199.       this.enterTextColor = this.getColor("enter_text_color");
  200.       if (this.enterTextColor == null) {
  201.          this.enterTextColor = Color.white;
  202.       }
  203.  
  204.    }
  205.  
  206.    public Color getColor(String param) {
  207.       String s = ((Applet)this).getParameter(param);
  208.       if (s != null) {
  209.          if (s.substring(0, 1).equals("#")) {
  210.             s = s.substring(1);
  211.             int i = Integer.parseInt(s, 16);
  212.             return new Color(i);
  213.          } else {
  214.             return null;
  215.          }
  216.       } else {
  217.          return null;
  218.       }
  219.    }
  220.  
  221.    public void start() {
  222.       if (this.thread == null) {
  223.          this.thread = new Thread(this);
  224.          this.thread.start();
  225.       }
  226.  
  227.    }
  228.  
  229.    public void stop() {
  230.       if (this.thread != null) {
  231.          this.thread = null;
  232.       }
  233.  
  234.       if (this.sound != null) {
  235.          this.sound.stop();
  236.       }
  237.  
  238.    }
  239.  
  240.    public final void paint(Graphics g) {
  241.       if (this.loaded) {
  242.          this.field_2.clearRect(0, 0, this.field_0, this.field_1);
  243.          this.field_2.drawImage(this.image, this.decx, this.decy, this);
  244.          if (this.isEnterTextEnabled) {
  245.             this.field_2.drawString(this.enterText, (this.field_0 - this.wiEnterText) / 2, this.field_1 / 2);
  246.          }
  247.  
  248.          g.drawImage(this.buffer, 0, 0, this);
  249.       }
  250.  
  251.    }
  252.  
  253.    public void run() {
  254.       if (!this.loaded) {
  255.          MediaTracker tracker = new MediaTracker(this);
  256.          this.image = ((Applet)this).getImage(((Applet)this).getCodeBase(), this.image_name);
  257.          tracker.addImage(this.image, 0);
  258.  
  259.          try {
  260.             tracker.waitForAll();
  261.             this.loaded = !tracker.isErrorAny();
  262.          } catch (InterruptedException var5) {
  263.             this.stop();
  264.          }
  265.  
  266.          if (!this.loaded) {
  267.             this.stop();
  268.             return;
  269.          }
  270.  
  271.          this.wi_im = this.image.getWidth(this);
  272.          this.he_im = this.image.getHeight(this);
  273.          this.decx = (this.field_0 - this.wi_im) / 2;
  274.          this.decy = (this.field_1 - this.he_im) / 2;
  275.          this.buffer = ((Component)this).createImage(this.field_0, this.field_1);
  276.          this.field_2 = this.buffer.getGraphics();
  277.          this.field_2.setColor(this.enterTextColor);
  278.          String fontName = ((Applet)this).getParameter("enter_text_font");
  279.          if (fontName == null) {
  280.             fontName = "TimesRoman";
  281.          }
  282.  
  283.          int style = 1;
  284.          String s = ((Applet)this).getParameter("enter_text_style");
  285.          if (s != null) {
  286.             if (s.equals("bold")) {
  287.                style = 1;
  288.             } else if (s.equals("italic")) {
  289.                style = 2;
  290.             } else if (s.equals("bold_italic")) {
  291.                style = 3;
  292.             } else {
  293.                style = 0;
  294.             }
  295.          }
  296.  
  297.          this.field_2.setFont(new Font(fontName, style, this.enterTextHeight));
  298.          this.field_3 = this.field_2.getFontMetrics();
  299.          if (this.enterText != null) {
  300.             this.wiEnterText = this.field_3.stringWidth(this.enterText);
  301.          }
  302.       }
  303.  
  304.       ((Component)this).repaint();
  305.    }
  306.  
  307.    public URL giveURL(String url) {
  308.       try {
  309.          if (url.toUpperCase().startsWith("HTTP")) {
  310.             return new URL(url);
  311.          } else if (url.toUpperCase().startsWith("FTP")) {
  312.             int p = url.indexOf(":");
  313.             int p2 = url.indexOf(":", p + 1);
  314.             if (p2 != -1) {
  315.                url = url.substring(0, p + 3) + URLEncoder.encode(url.substring(p + 3, url.length()));
  316.             }
  317.  
  318.             p = url.indexOf("%40");
  319.             if (p != -1) {
  320.                url = url.substring(0, p) + "@" + url.substring(p + 3, url.length());
  321.             }
  322.  
  323.             return new URL(url);
  324.          } else {
  325.             return new URL(((Applet)this).getCodeBase(), url);
  326.          }
  327.       } catch (MalformedURLException e) {
  328.          System.out.println(e);
  329.          return null;
  330.       }
  331.    }
  332.  
  333.    public void mouseClicked(MouseEvent e) {
  334.    }
  335.  
  336.    public void mouseEntered(MouseEvent e) {
  337.       if (this.enterText != null) {
  338.          this.isEnterTextEnabled = true;
  339.          ((Component)this).repaint();
  340.       }
  341.  
  342.       if (this.statusBarText != null) {
  343.          ((Applet)this).showStatus(this.statusBarText);
  344.       }
  345.  
  346.       if (this.enterSound != null) {
  347.          this.enterSound.play();
  348.       }
  349.  
  350.       if (this.link != null) {
  351.          ((Component)this).setCursor(new Cursor(12));
  352.       }
  353.  
  354.    }
  355.  
  356.    public void mouseExited(MouseEvent e) {
  357.       if (this.isEnterTextEnabled) {
  358.          this.isEnterTextEnabled = false;
  359.          ((Component)this).repaint();
  360.       }
  361.  
  362.       if (this.link != null) {
  363.          ((Component)this).setCursor(new Cursor(0));
  364.       }
  365.  
  366.    }
  367.  
  368.    public void mousePressed(MouseEvent e) {
  369.       if (this.clicSound != null) {
  370.          this.clicSound.play();
  371.       }
  372.  
  373.       if (this.link != null) {
  374.          String target = ((Applet)this).getParameter("target");
  375.          if (target == null) {
  376.             target = "_blank";
  377.          }
  378.  
  379.          URL u = this.giveURL(this.link);
  380.          ((Applet)this).getAppletContext().showDocument(u, target);
  381.       }
  382.  
  383.    }
  384.  
  385.    public void mouseReleased(MouseEvent e) {
  386.    }
  387. }
  388.